package com.csiec.clock.utils;

import android.annotation.SuppressLint;

import com.csiec.clock.bean.MyClock;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

public class TimeGet {
    //获取当前时间
    public static String getDate() {
        Date dNow = new Date();
        SimpleDateFormat ft =
                new SimpleDateFormat("yyyy.MM.dd HH:mm:ss");
        return ft.format(dNow);

    }

    //获取唯一id
    public static String getUId() {
        long time = System.currentTimeMillis();
        String s = String.valueOf(time);
        s = s.substring(3, 13);
        long id = Long.valueOf(s);
        System.out.println(s.length());
        return String.valueOf(id);
    }

    //获取随机字符串
    public static String getRandomString(int length) {
        Random random = new Random();
        StringBuffer sb = new StringBuffer();
        for (int i = 0; i < length; i++) {
            int number = random.nextInt(3);
            long result = 0;
            switch (number) {
                case 0:
                    result = Math.round(Math.random() * 25 + 65);
                    sb.append(String.valueOf((char) result));
                    break;
                case 1:
                    result = Math.round(Math.random() * 25 + 97);
                    sb.append(String.valueOf((char) result));
                    break;
                case 2:
                    sb.append(String.valueOf(new Random().nextInt(10)));
                    break;
            }
        }
        return sb.toString().toUpperCase();
    }


    /**
     * 日期类型转为字符串
     *
     * @param data
     * @return
     */
    @SuppressLint("SimpleDateFormat")
    public static String date2String(long data) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date resDate = new Date(data);
        return simpleDateFormat.format(resDate);
    }

    /**
     * 字符串转日期类型
     *
     * @param source
     * @param type   打卡类型
     * @return
     */
    @SuppressLint("SimpleDateFormat")
    public static Date string2Date(String source, String type) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        if (MyClock.CLOCKTYPE.equals(type)) {
            simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        }
        try {
            return simpleDateFormat.parse(source);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

}
